var a = require("../template/picker-template/class/ParseChe-class"), e = require("../template/picker-chepai-template/class/PickerChepai-class"), t = require("../../../../utils/checkData.js"), r = require("../../../../utils/util.js"), c = getApp(), i = new t.Check();

Page({
    data: {
        chepai: "苏",
        zimu: "A",
        isShowPicker: !1
    },
    onLoad: function(t) {
        new e.PickerChepai(this), new a.ParseChe(this);
        var r = JSON.parse(t.list);
        this.setData({
            chepai: r.carNo.charAt(0),
            zimu: r.carNo.charAt(1),
            carId: r.id,
            chenum: r.carNo.substr(2),
            car: r.brandname,
            color: r.color,
            date: r.buydate,
            brandId: r.brandId
        });
    },
    formatData: function(a) {
        return a.chenum = this.parseChepai(a.carpai), a.car = this.parseChexing(a.car), 
        a.color = a.color, a;
    },
    parseChexing: function(a) {
        var e = c.g_baseSql.cheData, t = this.data.value;
        for (var r in e) if (e[r].name === a) {
            t[1] = r;
            break;
        }
        return this.setData({
            car: a,
            value: t
        }), a;
    },
    parseChepai: function(a) {
        var e = a, t = c.g_baseSql.chepais, r = c.g_baseSql.zimus, i = a.substr(0, 1), n = a.substr(1, 1), o = [ 0, 0 ];
        for (var s in t) if (t[s] === i) {
            o[0] = parseInt(s);
            break;
        }
        for (var u in r) if (r[u] === n) {
            o[1] = parseInt(u);
            break;
        }
        return this.setData({
            chepai: i,
            zimu: n,
            value2: o
        }), e.substr(2, 6);
    },
    del: function(a) {
        var e = this;
        r.request(c, "wx/car/del", "POST", {
            token: c.globalData.token,
            carId: e.data.carId
        }, function(a) {
            wx.showModal({
                content: "删除成功"
            }), setTimeout(function() {
                wx.navigateBack();
            }, 1500);
        }, function(a) {});
    },
    submit: function(a) {
        var e = this.data, t = {
            chepai: e.chepai + e.zimu + e.chenum,
            color: e.color,
            date: e.date,
            brandId: e.brandId,
            car: e.car,
            carId: e.carId
        }, i = this.checkPostData(t);
        if (1 != i) return wx.showModal({
            title: "警告",
            content: i
        }), 0;
        if ("number" != typeof (n = t.date) && null != n) var n = parseInt(n.replace("-", ""));
        r.request(c, "wx/car/edit", "POST", {
            token: c.globalData.token,
            carId: t.carId,
            brandId: null == t.brandId ? 500 : t.brandId,
            carNo: t.chepai,
            color: null == t.color ? "默认颜色" : t.color,
            buyDate: null == n ? 201801 : n
        }, function(a) {
            "修改成功" == a.message && 1 == a.state && (wx.showModal({
                content: "修改成功 "
            }), setTimeout(function() {
                wx.navigateBack();
            }, 1500));
        }, function(a) {});
    },
    checkPostData: function(a) {
        var e = i.checkChepai(a.chepai);
        if (1 != e) return e;
        var t = i.checkCar(a.car);
        if (1 != t) return t;
        var r = i.checkColor(a.color);
        if (1 != r) return r;
        var c = i.checkDate(a.date);
        return 1 == c || c;
    },
    getColor: function(a) {
        this.data.color = a.detail.value;
    },
    getCheNum: function(a) {
        this.data.chenum = a.detail.value;
    },
    onReady: function() {
        this.setData({
            ishow: "block"
        });
    },
    bindDateChange: function(a) {
        this.setData({
            date: a.detail.value
        });
    }
});